/**
  ******************************************************************************
  * File Name          : esp8266_common.c
  * Description        : This file provides code for the configuration
  *                      of the ESP8266 module.
  ******************************************************************************
  * @attention
  * Created by Chen Lu in 20210531
  *
  ******************************************************************************
  */

#include "stdlib.h"
#include "string.h"
#include "main.h"
#include "esp8266_common.h"
#include "usart.h"

extern DMA_HandleTypeDef hdma_usart2_rx;

int str_len(uint8_t* str)
{
	int len;
	uint8_t* p=str;
	while(*p!='\0')
		p++;
	len = p-str;
	return len;
}

//ESP8266 initial
void esp8266_construct(DMARxTxStructure *huartrxtx, uint8_t* buf)
{
	huartrxtx->len = 0;
	huartrxtx->p_data = buf;
}

int esp8266_init(void)
{
	esp8266_construct(&USART2_RX,USART2_RX_BUF);
	esp8266_construct(&USART2_TX,USART2_TX_BUF);
	return ESP8266_OK;
}

//ESP8266 response at usmart support
void esp8266_at_response(uint8_t mode)
{
	if (USART2_RX.len>0)
	{
		*(USART2_RX.p_data + USART2_RX.len) = '\0';
		printf("%s\n",USART2_RX.p_data);
		//usage of "mode" variable is not tested
		if (mode)
			memset(&USART2_RX.p_data,0x00,sizeof(USART2_RX_BUF));
	}
}

//check the position of response to the command
uint8_t* esp8266_check_cmd(uint8_t *str)
{
	char *strx=0;
	if (USART2_RX.len>0)
	{
		*(USART2_RX.p_data + USART2_RX.len) = '\0';
		strx=strstr((const char*)USART2_RX.p_data,(const char*)str);
	}
	return (uint8_t*)strx;
}

//send command to esp8266
uint8_t esp8266_send_cmd(uint8_t *cmd,uint16_t waittime)
{
	uint8_t res=0;

	USART2_TX.len = strlen((const char*)cmd);
	strcpy((char*)USART2_TX.p_data,(char*)cmd);
	*(USART2_TX.p_data+USART2_TX.len) = '\r';
	*(USART2_TX.p_data+USART2_TX.len+1) = '\n';
	USART2_TX.len += 2;

	HAL_UART_Transmit_DMA(&huart2, (uint8_t*)USART2_TX.p_data, USART2_TX.len);
	while(--waittime)	//waiting count
	{
		HAL_Delay(50);
		if (USART2_RX.len>0)
		{
			*(USART2_RX.p_data+USART2_RX.len) = '\0';
			printf("%s\r\n",USART2_RX.p_data);
			USART2_RX.len = 0;
			memset(USART2_RX.p_data,0x00,sizeof(USART2_RX_BUF));
			break;//get response then break
		}
	}
	if (waittime == 0)
		res=1;

	return res;
}

//send data through esp8266
//notice:no need "\r\n"
uint8_t esp8266_send_data(uint8_t *data,uint16_t waittime)
{
	uint8_t res=0;

	USART2_TX.len = strlen((const char*)data);
	strcpy((char*)USART2_TX.p_data,(char*)data);

	HAL_UART_Transmit_DMA(&huart2, (uint8_t*)USART2_TX.p_data, USART2_TX.len);
	while(--waittime)	//waiting count
	{
		HAL_Delay(50);
		if (USART2_RX.len>0)
		{
			*(USART2_RX.p_data+USART2_RX.len) = '\0';
			printf("%s\r\n",USART2_RX.p_data);
			USART2_RX.len = 0;
			memset(&USART2_RX.p_data,0x00,sizeof(USART2_RX_BUF));
			break;//get response then break
		}
	}
	if (waittime==0)
		res=1;

	return res;
}

//quit pass-through mode
uint8_t esp8266_quit_trans(void)
{
	esp8266_send_cmd((uint8_t*)PASS_THROUGH_QUIT,20);
	return esp8266_send_cmd((uint8_t*)AT,20);//quit pass-through judgment
}

//get AP+STA connection status of esp8266
//1:success
//0:fail
//uint8_t esp8266_apsta_check(void)
//{
//	if (esp8266_quit_trans())
//		return 0;			//quit pass-through
//	esp8266_send_cmd((uint8_t*)AT_CIPSTATUS,(uint8_t*)":",50);	//send AT+CIPSTATUS command
//	if(esp8266_check_cmd((uint8_t*)"+CIPSTATUS:0")&&
//		esp8266_check_cmd((uint8_t*)"+CIPSTATUS:1")&&
//		esp8266_check_cmd((uint8_t*)"+CIPSTATUS:2")&&
//		esp8266_check_cmd((uint8_t*)"+CIPSTATUS:4"))
//		return 0;
//	else return 1;
//}

//get connection status of esp8266
//1:success
//0:fail
//uint8_t esp8266_consta_check(void)
//{
//	uint8_t *p;
//	uint8_t res;
//	if (esp8266_quit_trans())
//		return 0;			//quit pass-through
//	esp8266_send_cmd((uint8_t*)AT_CIPSTATUS,(uint8_t*)":",50);	//send AT+CIPSTATUS command
//	p=esp8266_check_cmd((uint8_t*)"+CIPSTATUS:");
//	res=*p;
//	return res;
//}
